package com.zb.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.router.SaRouterStaff;
import cn.dev33.satoken.stp.StpUtil;
import com.zb.config.inteceptor.NetworkInteceptor;
import com.zb.config.inteceptor.SysPCInteceptor;
import com.zb.config.inteceptor.UserInteceptor;
import com.zb.service.ZjUserSetService;
import com.zb.stp.StpNetworkUtil;
import com.zb.stp.StpUserUtil;
import com.zb.utils.IgnorePathUtil;
import com.zb.utils.RequestUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;


/**
 * @Author WEN
 * @Date 2023/7/19 0019
 **/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private ZjUserSetService userSetService;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handler -> {
            SaRouterStaff match = SaRouter.match("/**").notMatch(IgnorePathUtil.ignorePath());
            // 工单报单移动端
            if (RequestUtil.isMobileUser()) {
                if (RequestUtil.isUserMobileHasLogin()) {
                    match.check(r -> StpUserUtil.checkLogin());
                }
                return;
            }
            // 服务商端
            if (RequestUtil.isMobileNetwork()) {
                match.check(r -> StpNetworkUtil.checkLogin());
                return;
            }
            // pc端
            if (RequestUtil.isPc()) {
                match.check(r -> StpUtil.checkLogin());
            }
        }));
        // 注册工单报单移动端拦截器
        registry.addInterceptor(new UserInteceptor(userSetService));
        // 服务商端
        registry.addInterceptor(new NetworkInteceptor());
        // 注册pc端拦截器
        registry.addInterceptor(new SysPCInteceptor());
    }
}
